package puj.promobil.prometer;

import java.util.ArrayList;

import puj.promobil.prometer.adapters.ItemProductoAdapter;
import puj.promobil.prometer.dto.Empresa;
import puj.promobil.prometer.persistence.DatabaseManager;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemSelectedListener;
//import android.widget.Toast;

public class ListenerSelectEmpresa implements OnItemSelectedListener {

	private Activity actividad = null;

	public ListenerSelectEmpresa(Activity at) {
		super();
		actividad = at;
	}

	@Override
	public void onItemSelected(AdapterView<?> parent, View arg1, int pos,
			long arg3) {
		

		ArrayList<Empresa> items;
		try {
			items = DatabaseManager.getInstance().getAllEmpresa();

			for (Empresa val : items) {
				
				
				if (val.getRazon().equals(parent.getItemAtPosition(pos).toString())) {
					//Toast.makeText(parent.getContext(),"Cantidada de productos : "+ val.getProductos().size(),	Toast.LENGTH_SHORT).show();

					ListView lista_productos = (ListView) actividad.findViewById(R.id.lista_productos);
					
					ItemProductoAdapter adapter = (ItemProductoAdapter) lista_productos.getAdapter();

					adapter.setItems(val.getProductos());
					
					return;
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {

	}

}
